package Group9.TodoManager;

import java.awt.Component;
import java.util.EventObject;
import javax.swing.JSlider;
import javax.swing.JTable;
import javax.swing.event.CellEditorListener;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.EventListenerList;
import javax.swing.table.TableCellEditor;

/**
 * SimpleCellEditor class is the cell editor for table model. How cells of the
 * table will be edited is defined in here.
 * @author Rupesh
 * @author volkan cambazoglu
 */
public class SimpleCellEditor extends JSlider implements TableCellEditor {

    protected EventListenerList listenerList;
    protected ChangeEvent changeEvent;
    
    /**
     * Default constructor of SimpleCellEditor. An instance of the class is
     * created. Data members are initialized properly.
     */
    public SimpleCellEditor() {
        super();
        this.setMajorTickSpacing(1);
        this.setMinimum(0);
        this.setMaximum(2);
        listenerList = new EventListenerList();
        changeEvent = new ChangeEvent(this);

    }

    /**
     * This method is used to get the component of the cell editor.
     * Attributes such as background and foreground of the component are
     * set.
     * @param table the table that the component belongs to
     * @param value
     * @param isSelected boolean value that represents if the component is selected
     * @param row the row of the table
     * @param column the column of the table
     * @return component the component to be retreived
     */
    public Component getTableCellEditorComponent(JTable table, Object value,
            boolean isSelected, int row, int column) {
    	
        if(isSelected) {
            setForeground(table.getSelectionForeground());
            super.setBackground(table.getSelectionBackground());
        }
        else {
            setForeground(table.getForeground());
            setBackground(table.getBackground());
        }

        this.addChangeListener(new ChangeListener() {
        	
        	/**
        	 * When state of the component is changed execute here
        	 */
            public void stateChanged(ChangeEvent e) {
                if(!((JSlider)e.getSource()).getValueIsAdjusting()) {
                    changeEvent = e;
                    stopCellEditing();                    
                }
            }
        });

        return this;
    }

    /**
     * Getter method  of the cell editor value
     * @return value the value
     */
    public Object getCellEditorValue() {
        return this.getValue();
    }

    /**
     * Getter method  of the boolean value of cell being editable or not
     * @param anEvent event
     * @return boolean true if yes, else false
     */
    public boolean isCellEditable(EventObject anEvent) {
        return true;
    }

    /**
     * Getter method  of the boolean value of cell should be selected or not
     * @param anEvent event
     * @return boolean true if yes, else false
     */
    public boolean shouldSelectCell(EventObject anEvent) {
        return true;
    }

    /**
     * Getter method  of the boolean value of stop cell editing 
     * @return boolean true if yes, else false
     */
    public boolean stopCellEditing() {
        for (Object obj : listenerList.getListenerList()) {
            if (obj instanceof CellEditorListener) {
                ((CellEditorListener) obj).editingStopped(changeEvent);
            }
        }
        return true;
    }

    /**
     * Getter method  of the boolean value of cancel cell editing 
     * @return boolean true if yes, else false
     */
    public void cancelCellEditing() {
        for (Object obj : listenerList.getListenerList()) {
            ((CellEditorListener) obj).editingCanceled(changeEvent);
        }
    }

    /**
     * Adds Listener to the cell editor class.
     * @param l listener to be added
     */
    public void addCellEditorListener(CellEditorListener l) {
        listenerList.add(CellEditorListener.class, l);
    }

    /**
     * Removes Listener from the cell editor class.
     * @param l listener to be removed
     */
    public void removeCellEditorListener(CellEditorListener l) {
        listenerList.remove(CellEditorListener.class, l);
    }
}
